package com.mcj.managementSystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcj.managementSystem.common.Result;

import com.mcj.managementSystem.service.IRoleService;
import com.mcj.managementSystem.entity.Role;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 妙脆角
 * @since 2022-05-29
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Role role) {
        roleService.saveOrUpdate(role);
		return Result.success();
	}

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        roleService.removeById(id);
		return Result.success();
	}

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        roleService.removeByIds(ids);
		return Result.success();
	}

    @GetMapping
    public Result findAll() {
		return Result.success(roleService.list());
	}

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
		return Result.success(roleService.getById(id));
	}

    @GetMapping("/page")
    public Result findPage(@RequestParam(required = false) String name,
						   @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
		QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
		if (StringUtils.hasText(name)) {
			queryWrapper.like("name", name);
		}
		return Result.success(roleService.page(new Page<>(pageNum, pageSize), queryWrapper));
	}
}

